package com.jpragma.blazeds.utils;

import javax.servlet.ServletConfig;

import flex.messaging.config.FlexConfigurationManager;

/**
 * Loads BlazeDS/LCDS configuration files from classpath.<br/>
 * Standard Adobe implementation requires to configure <b>flex.messaging.MessageBrokerServlet</b> in web.xml,
 * providing relative path to service-config.xml
 * <pre>
   &lt;init-param&gt;
     &lt;param-name&gt;services.configuration.file&lt;/param-name&gt;
     &lt;param-value&gt;/WEB-INF/flex/services-config.xml&lt;/param-value&gt;
   &lt;/init-param&gt;
 * </pre>
 * Using this class one can place configuration files (service-config.xml, remoting-config.xml, etc.) 
 * anywhere in the classpath. Maven projects can place them in <i>src/main/resources</i><br/> 
 * Configuration (web.xml):
 * <pre>
    &lt;servlet&gt;
        &lt;servlet-name&gt;MessageBrokerServlet&lt;/servlet-name&gt;
        &lt;display-name&gt;MessageBrokerServlet&lt;/display-name&gt;
        &lt;servlet-class&gt;flex.messaging.MessageBrokerServlet&lt;/servlet-class&gt;
        &lt;init-param&gt;
        	&lt;param-name&gt;services.configuration.manager&lt;/param-name&gt;
        	&lt;param-value&gt;com.jpragma.blazeds.utils.ClasspathFlexConfigurationManager&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;services.configuration.file&lt;/param-name&gt;
            &lt;param-value&gt;services-config.xml&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
 * </pre>
 *  
 * @author Isaac Levin
  */
public class ClasspathFlexConfigurationManager extends FlexConfigurationManager {
	
	protected void setupConfigurationPathAndResolver(ServletConfig servletConfig) {
		String defaultFile = "services-config.xml";
		if (servletConfig != null) {
			String p = servletConfig.getInitParameter("services.configuration.file");
			if(p == null || p.trim().length() == 0) {
				configurationPath = defaultFile;
			} else {
				configurationPath = p.trim();
			}
		} else {
			configurationPath = defaultFile;
		}
		configurationResolver = new ClasspathConfigurationFileResolver();
	}
}
